package njupt.Servlet;

import java.io.IOException;

import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import njupt.vo.pagination;

import factory.DAOFactory;

public class PaginationServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		req.setCharacterEncoding("GB2312");
		resp.setCharacterEncoding("GB2312");
		resp.setContentType("text/html;charset=GB2312");
		String action=req.getParameter("action");
		HttpSession session=req.getSession(true);
		pagination pg = (pagination)session.getAttribute("pg");
		if(pg==null){
		   pg=new pagination();
		    session.setAttribute("pg",pg);
		}
		if(action==null){return;}
		else if(action.equals("xianshi")){
		try{
			    pg.setCurrentpage(1);
			    pg.setTotalpage(DAOFactory.getUserDAOinstance().counttotalpage()) ;      //设置总页数到bean
			    System.out.println("Totalpage总共页数："+DAOFactory.getUserDAOinstance().counttotalpage());
			 //   String path="showuser.jsp";
			    System.out.println("DAOFactory.getUserDAOinstance().showuser()");
			    if(DAOFactory.getUserDAOinstance().showuser(pg.getCurrentpage(),pg.getPagesize())!=null){
				 List<String[]> usermsg =DAOFactory.getUserDAOinstance().showuser(pg.getCurrentpage(),pg.getPagesize());
				   req.setAttribute("usermsg", usermsg);
			//	   System.out.println("【paginationServlet1】pg.getCurrentpage()"+pg.getCurrentpage());
				   req.getRequestDispatcher("showuser.jsp").forward(req, resp);
			    }
	
			 
		}catch(Exception e){
			
			e.printStackTrace();	
	         }
		}	
		else if(action.equals("xiayiye")){
			try{
				   // pg.setTotalpage(DAOFactory.getUserDAOinstance().counttotalpage()) ;
				   // System.out.println("Totalpage总共页数："+DAOFactory.getUserDAOinstance().counttotalpage());
				    //String path="showuser.jsp";
				    pg.setCurrentpage(pg.getCurrentpage()+1);
				    System.out.println("【】pg.getCurrentpage()"+pg.getCurrentpage());
				    System.out.println("DAOFactory.getUserDAOinstance().showuser()");
				    if(DAOFactory.getUserDAOinstance().showuser(pg.getCurrentpage(),pg.getPagesize())!=null){
					List<String[]> usermsg =DAOFactory.getUserDAOinstance().showuser(pg.getCurrentpage(),pg.getPagesize());
					req.setAttribute("usermsg", usermsg);
					req.getRequestDispatcher("showuser.jsp").forward(req, resp);
				    }	 	 
			  }catch(Exception e){
				
				e.printStackTrace();	
		         }
		}
		else if(action.equals("shangyiye")){
			try{
				   // pg.setTotalpage(DAOFactory.getUserDAOinstance().counttotalpage()) ;
				   // System.out.println("Totalpage总共页数："+DAOFactory.getUserDAOinstance().counttotalpage());
				    //String path="showuser.jsp";
				    pg.setCurrentpage(pg.getCurrentpage()-1);
				    //System.out.println("【】pg.getCurrentpage()"+pg.getCurrentpage());
				    System.out.println("DAOFactory.getUserDAOinstance().showuser()");
				    if(DAOFactory.getUserDAOinstance().showuser(pg.getCurrentpage(),pg.getPagesize())!=null){
					List<String[]> usermsg =DAOFactory.getUserDAOinstance().showuser(pg.getCurrentpage(),pg.getPagesize());
					req.setAttribute("usermsg", usermsg);
					req.getRequestDispatcher("showuser.jsp").forward(req, resp);
				    }	 	 
			  }catch(Exception e){
				
				e.printStackTrace();	
		         }
		}
		
		else if(action.equals("xuanye")){
			try{
				   // pg.setTotalpage(DAOFactory.getUserDAOinstance().counttotalpage()) ;
				   // System.out.println("Totalpage总共页数："+DAOFactory.getUserDAOinstance().counttotalpage());
				    //String path="showuser.jsp";
				    String currentpage=(String)req.getParameter("currentpage");
				   
				    int currentpages=Integer.parseInt(currentpage);
				    System.out.println("currentpage"+currentpages);
				    pg.setCurrentpage(currentpages);
				    //System.out.println("【】pg.getCurrentpage()"+pg.getCurrentpage());
				    System.out.println("DAOFactory.getUserDAOinstance().showuser()");
				    if(DAOFactory.getUserDAOinstance().showuser(pg.getCurrentpage(),pg.getPagesize())!=null){
					List<String[]> usermsg =DAOFactory.getUserDAOinstance().showuser(pg.getCurrentpage(),pg.getPagesize());
					req.setAttribute("usermsg", usermsg);
					req.getRequestDispatcher("showuser.jsp").forward(req, resp);
				    }	 	 
			  }catch(Exception e){
				
				e.printStackTrace();	
		         }
		}
		
	}
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		 this.doGet(req, resp);
	}

}
